﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cua_Hang.DTO;
using System.Data;

namespace Cua_Hang.DAO
{
    public class DiaChi_CuaHang_DAO
    {
        //-------load dia chi cua Hang------------
        public static List<DiaChi_CuaHang_DTO> List_DiaChiCuaHang_DAO(int ID_cuahang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang AS T1 JOIN DiaChi_CuaHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang WHERE T2.ID_cuahang like '%" + ID_cuahang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<DiaChi_CuaHang_DTO> lstDiaChiCuaHang = new List<DiaChi_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    DiaChi_CuaHang_DTO dm = new DiaChi_CuaHang_DTO();
                    dm.ID_ThongTin_CuaHang = (int)dr["ID_ThongTin_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_CuaHang = dr["Ten_CuaHang"].ToString();
                    dm.Duong_CuaHang = dr["Duong_CuaHang"].ToString();
                    dm.Phuong_CuaHang = dr["Phuong_CuaHang"].ToString();
                    dm.Quan_CuaHang = dr["Quan_CuaHang"].ToString();
                    dm.Tp_CuaHang = dr["Tp_CuaHang"].ToString();
                    dm.Email_CuaHang = dr["Email_CuaHang"].ToString();
                    //dm.DienThoai_Cuahang = (int)dr["DienThoai_Cuahang"];


                    lstDiaChiCuaHang.Add(dm);
                }
                return lstDiaChiCuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load dia chi cua Hang-----------
        //==================================================================
        //-------load tat ca dia chi cua Hang------------
        public static List<DiaChi_CuaHang_DTO> List_DiaChi_TatCa_CuaHang_DAO()
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT T2.ID_ThongTin_CuaHang,T2.ID_cuahang,T2.Ten_CuaHang,T2.Duong_CuaHang,T2.Phuong_CuaHang,T2.Quan_CuaHang,T2.Tp_CuaHang,T2.Email_CuaHang,T2.DienThoai_Cuahang FROM Cua_Hang AS T1 JOIN DiaChi_CuaHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang ";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<DiaChi_CuaHang_DTO> lstDiaChiCuaHang = new List<DiaChi_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    DiaChi_CuaHang_DTO dm = new DiaChi_CuaHang_DTO();
                    dm.ID_ThongTin_CuaHang = (int)dr["ID_ThongTin_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_CuaHang = dr["Ten_CuaHang"].ToString();
                    //dm.Ten_CuaHangChinh = dr["Ten_cuahang"].ToString();
                    dm.Duong_CuaHang = dr["Duong_CuaHang"].ToString();
                    dm.Phuong_CuaHang = dr["Phuong_CuaHang"].ToString();
                    dm.Quan_CuaHang = dr["Quan_CuaHang"].ToString();
                    dm.Tp_CuaHang = dr["Tp_CuaHang"].ToString();
                    dm.Email_CuaHang = dr["Email_CuaHang"].ToString();
                    dm.DienThoai_Cuahang =Convert.ToInt64(dr["DienThoai_Cuahang"]);


                    lstDiaChiCuaHang.Add(dm);
                }
                return lstDiaChiCuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load tat ca dia chi cua Hang-----------
        //==================================================================
        //==================================================================
        //-----------them dia chi cua hang ------------

        public static void Them_DiaChi_CuaHang_DAO(DiaChi_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "INSERT INTO DiaChi_CuaHang(ID_cuahang,Duong_CuaHang,DienThoai_Cuahang,Email_CuaHang,Ten_CuaHang,Phuong_CuaHang,Quan_CuaHang,Tp_CuaHang) VALUES(";
                //strSQL += "N'" + kh.Ten_KhachHang + "',";
                //strSQL += "N'" + kh.DiaChi_KhachHang + "',";
                //strSQL += "N'" + kh.Email_KhachHang + "',";
                strSQL += "N'" + ch.ID_cuahang + "',";
                strSQL += "N'" + ch.Duong_CuaHang + "',";
                strSQL += "N'" + ch.DienThoai_Cuahang + "',";
                strSQL += "N'" + ch.Email_CuaHang + "',";
                strSQL += "N'" + ch.Ten_CuaHang + "',";
                strSQL += "N'" + ch.Phuong_CuaHang + "',";
                strSQL += "N'" + ch.Quan_CuaHang+ "',";         
                strSQL += "N'" + ch.Tp_CuaHang + "')";
                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }


        //-------ket thuc them dia chi cua hang  ------------
        //==============================================================
        //=============================================================
        //------ xoa dia chi cua hang ------------
        public static void Xoa_DiaChi_CuaHang_DAO(DiaChi_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "DELETE FROM DiaChi_CuaHang WHERE ID_ThongTin_CuaHang ='" + ch.ID_ThongTin_CuaHang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc xoa dia chi cua hang------------

        //==============================================================
        //------ sua chi tiet cua hang  ------------
        public static void Sua_DiaChi_CuaHang_DAO(DiaChi_CuaHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "UPDATE DiaChi_CuaHang SET Duong_CuaHang = N'" + ch.Duong_CuaHang + "',DienThoai_Cuahang=N'" + ch.DienThoai_Cuahang + "',Email_CuaHang=N'" + ch.Email_CuaHang + "',Ten_CuaHang=N'" + ch.Ten_CuaHang + "',Phuong_CuaHang=N'" + ch.Phuong_CuaHang + "',Quan_CuaHang=N'" + ch.Quan_CuaHang + "',Tp_CuaHang=N'" + ch.Tp_CuaHang + "' WHERE ID_ThongTin_CuaHang = '" + ch.ID_ThongTin_CuaHang + "'";
                //string strSQL = "UPDATE ChiTiet_CuaHang " +
                //"SET Ten_cuahang = '" + ch.Ten_cuahang +

                //" DiaChi = '" + info.DiaChi + "', " +
                //" NgaySinh = '" + info.NgaySinh.ToShortDateString() + "', " +
                //" MaLop = '" + info.MaLop + "'," +
                //" GioiTinh = '" + info.GioiTinh + "', " +
                //" NamHoc = " + info.NamHoc +
                // " WHERE ID_cuahang = '" + ch.ID_cuahang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //==============================================================
        //------ ham kiem tra toa do cua hang co trong csdl hay chua ------------
        public static Boolean KiemTraTrung_DiaChi_CuaHang_DAO(DiaChi_CuaHang_DTO ch)
        {
            Boolean kq = false;
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM DiaChi_CuaHang  WHERE Ten_CuaHang =N'" + ch.Ten_CuaHang + "' AND Email_CuaHang =N'" + ch.Email_CuaHang + "'";
                DataTable dt = d.ExecuteQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    kq = true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
            return kq;
        }
        //-------ket thuc ham kiem tra toa do cua hang co trong csdl hay chua -----------
        //-------load dia chi cua Hang theo id------------
        public static List<DiaChi_CuaHang_DTO> List_DiaChi_TheoID_DAO(int ID_ThongTin_CuaHang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT T2.ID_cuahang,T2.Ten_CuaHang,T2.Duong_CuaHang,T2.Phuong_CuaHang,T2.Quan_CuaHang,T2.Tp_CuaHang,T2.Email_CuaHang,T2.DienThoai_Cuahang FROM Cua_Hang AS T1 JOIN DiaChi_CuaHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang WHERE T2.ID_ThongTin_CuaHang like '%" + ID_ThongTin_CuaHang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<DiaChi_CuaHang_DTO> lstDiaChiCuaHang = new List<DiaChi_CuaHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    DiaChi_CuaHang_DTO dm = new DiaChi_CuaHang_DTO();
                    //dm.ID_ThongTin_CuaHang = (int)dr["ID_ThongTin_CuaHang"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.Ten_CuaHang = dr["Ten_CuaHang"].ToString();
                    //dm.Ten_CuaHangChinh = dr["Ten_cuahang"].ToString();
                    dm.Duong_CuaHang = dr["Duong_CuaHang"].ToString();
                    dm.Phuong_CuaHang = dr["Phuong_CuaHang"].ToString();
                    dm.Quan_CuaHang = dr["Quan_CuaHang"].ToString();
                    dm.Tp_CuaHang = dr["Tp_CuaHang"].ToString();
                    dm.Email_CuaHang = dr["Email_CuaHang"].ToString();
                    dm.DienThoai_Cuahang = Convert.ToInt64(dr["DienThoai_Cuahang"]);


                    lstDiaChiCuaHang.Add(dm);
                }
                return lstDiaChiCuaHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load dia chi cua Hang theo id-----------
        //==================================================================





    }
}